'use strict';
const dayjs = require("dayjs");
module.exports = app => {
  const { STRING, INTEGER, TEXT, DECIMAL } = app.Sequelize;
  const OrderGoods = app.model.define(
      'orderGoods',
      {
          id: {type: INTEGER, primaryKey: true, autoIncrement: true},
            order_no: {type: STRING(255),  comment: '订单号'},
            staff_id: {type: INTEGER,  comment: '客服'},
            member_id: {type: INTEGER,  comment: '用户'},
            address: {type: TEXT,  comment: '地址'},
            staff_address: {type: TEXT, allowNull: true, comment: '阿姨地址'},
            start_time: {type: STRING(255),  comment: '预约开始时间'},
            end_time: {type: STRING(255),  comment: '预约结束时间'},
            remark: {type: TEXT, allowNull: true, comment: '预约备注'},
            travel_mode_id: {type: INTEGER, allowNull: true, comment: '出行方式'},
            travel_mode_name: {type: STRING(255), allowNull: true, comment: '出行方式名称'},
            travel_mode_fee: {type: DECIMAL(10, 2), allowNull: true, comment: '出行方式费用'},
            order_status: {type: INTEGER, allowNull: true, comment: '订单状态'},
            pay_status: {type: INTEGER, allowNull: true, comment: '支付状态'},
            pay_type: {type: INTEGER, allowNull: true, comment: '支付方式'},
            pay_amount: {type: DECIMAL(10, 2), allowNull: true, comment: '支付金额'},
            discount_amount: {type: DECIMAL(10, 2), allowNull: true, comment: '优惠金额'},
          // 阿姨结算总价
            staff_amount: {type: DECIMAL(10, 2), allowNull: true, comment: '阿姨结算总价'},
            // 阿姨结算比例
            staff_ratio: {type: INTEGER, allowNull: true, comment: '阿姨结算比例'},
            is_del: {type: INTEGER, allowNull: true, comment: '是否删除'},
            totalkm: {type: STRING, allowNull: true, comment: '总公里数'},
            // 结算状态
            settlement_status: {type: INTEGER, allowNull: true, comment: '结算状态'},
          back_result: STRING(255),
          back_reason: STRING(255),
          back_img: STRING,
          back_amount: DECIMAL(10,2),
          back_no: STRING(255),
          refund_id: STRING(255),
          accept_time: {
              type: app.Sequelize.DATE,
              allowNull: true,
              get() {
                  return dayjs(this.getDataValue('serve_time')).format('YYYY-MM-DD HH:mm:ss');
              },
          },
          pay_time: {
              type: app.Sequelize.DATE,
              allowNull: true,
              get() {
                  if (this.getDataValue('pay_time') === 'Invalid Date' || !this.getDataValue('pay_time')) {
                      return  ''
                  }
                  return dayjs(this.getDataValue('pay_time')).format('YYYY-MM-DD HH:mm:ss');
              },
          },
          serve_end_time: {
              type: app.Sequelize.DATE,
              allowNull: true,
              get() {
                  if (this.getDataValue('serve_end_time') === 'Invalid Date' || !this.getDataValue('serve_end_time')) {
                      return  ''
                  }
                  return dayjs(this.getDataValue('serve_end_time')).format('YYYY-MM-DD HH:mm:ss');
              },
          },
        serve_time: {
              type: app.Sequelize.DATE,
              allowNull: true,
              get() {
                  return dayjs(this.getDataValue('serve_time')).format('YYYY-MM-DD HH:mm:ss');
              },
          },
          create_time: {
              type: app.Sequelize.DATE,
              
              get() {
                  return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
              },
          },
          updated_time: {
              type: app.Sequelize.DATE,
              
              get() {
                  return dayjs(this.getDataValue('updated_time')).format('YYYY-MM-DD HH:mm:ss');
              },
          }
      },
      {
          tableName: 'order',
          timestamps: true, // 启用时间戳(createdAt, updatedAt)
          updatedAt: 'updated_time', // 不想要 updatedAt
          createdAt: 'create_time', // 想要  createdAt 但是希望名称叫做 created_at
      }
  );
    OrderGoods.associate = function () {
        OrderGoods.hasMany(app.model.OrderGoodsDetail, { foreignKey: 'order_id', sourceKey: 'id'}) // 订单与订单详情的关系定义
        // member
        OrderGoods.belongsTo(app.model.Member, { foreignKey: 'member_id', targetKey: 'id'}) // 订单与用户的关系定义
        // staff
        OrderGoods.belongsTo(app.model.Staff, { foreignKey: 'staff_id', targetKey: 'id'}) // 订单与客服的关系定义
    }
    return OrderGoods
};
